/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Liste.java
 *
 * Created on 1 juil. 2011, 10:30:21
 */
package org.lestr.otaf.ihm;

import javax.swing.DefaultListModel;

/**
 *
 * @author pibonnin
 */
public class Liste extends javax.swing.JPanel {


    /** Creates new form Liste */
    public Liste() {
        initComponents();
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        liste = new javax.swing.JList();
        supprimer = new javax.swing.JButton();
        definir = new javax.swing.JButton();
        champ = new javax.swing.JTextField();
        panneauSansMarge = new javax.swing.JPanel();
        panneau = new javax.swing.JPanel();
        etiquette = new javax.swing.JLabel();

        setBorder(javax.swing.BorderFactory.createEtchedBorder());

        liste.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                listeValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(liste);

        supprimer.setText("Supprimer");
        supprimer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                supprimerActionPerformed(evt);
            }
        });

        definir.setText("Ajouter / Modifier");
        definir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                definirActionPerformed(evt);
            }
        });

        panneau.setLayout(new javax.swing.BoxLayout(panneau, javax.swing.BoxLayout.Y_AXIS));

        javax.swing.GroupLayout panneauSansMargeLayout = new javax.swing.GroupLayout(panneauSansMarge);
        panneauSansMarge.setLayout(panneauSansMargeLayout);
        panneauSansMargeLayout.setHorizontalGroup(
            panneauSansMargeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panneauSansMargeLayout.createSequentialGroup()
                .addComponent(panneau, javax.swing.GroupLayout.DEFAULT_SIZE, 681, Short.MAX_VALUE)
                .addContainerGap())
        );
        panneauSansMargeLayout.setVerticalGroup(
            panneauSansMargeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panneauSansMargeLayout.createSequentialGroup()
                .addComponent(panneau, javax.swing.GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE)
                .addContainerGap())
        );

        etiquette.setText("jLabel1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(panneauSansMarge, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(etiquette)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(champ, javax.swing.GroupLayout.DEFAULT_SIZE, 443, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(definir)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(supprimer))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 691, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(champ, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(supprimer)
                    .addComponent(definir)
                    .addComponent(etiquette))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panneauSansMarge, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void definirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_definirActionPerformed

        DefaultListModel modele = (DefaultListModel) liste.getModel();
            
        if(liste.getSelectedIndex() == -1) {
            
            modele.addElement(champ.getText());
            
        }
        else {
            
            modele.setElementAt(champ.getText(), liste.getSelectedIndex());
            
        }
        
    }//GEN-LAST:event_definirActionPerformed

    private void supprimerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_supprimerActionPerformed

        DefaultListModel modele = (DefaultListModel) liste.getModel();
            
        if(liste.getSelectedIndex() != -1) {
            
            modele.removeElementAt(liste.getSelectedIndex());
            
        }
        
    }//GEN-LAST:event_supprimerActionPerformed

    private void listeValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_listeValueChanged
        
        
        
    }//GEN-LAST:event_listeValueChanged


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField champ;
    private javax.swing.JButton definir;
    private javax.swing.JLabel etiquette;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList liste;
    private javax.swing.JPanel panneau;
    private javax.swing.JPanel panneauSansMarge;
    private javax.swing.JButton supprimer;
    // End of variables declaration//GEN-END:variables
}

